<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>控制台 - <?= $systemConfig['system_name'] ?? '' ?></title>
<link rel="stylesheet" href="/layui/css/layui.css">
<script src="/static/js/jquery.min.js"></script>
<script src="/layui/layui.js"></script>
<style>
*{margin:0;padding:0;box-sizing:border-box;}
body{background:#f5f7fa;font-family:"Microsoft YaHei","PingFang SC",sans-serif;}
.header{background:#fff;box-shadow:0 1px 4px rgba(0,0,0,.08);}
.header .inner{max-width:1200px;margin:0 auto;display:flex;align-items:center;justify-content:space-between;padding:0 20px;height:60px;}
.header .logo{font-size:20px;font-weight:700;color:#1e3c72;text-decoration:none;}
.header .nav{display:flex;gap:16px;}
.header .nav a{color:#555;text-decoration:none;font-size:14px;padding:8px 12px;border-radius:4px;}
.header .nav a:hover,.header .nav a.active{color:#1e9fff;background:#f0f7ff;}
.container{max-width:1200px;margin:0 auto;padding:20px;}
.stat-row{display:flex;gap:16px;margin-bottom:20px;flex-wrap:wrap;}
.stat-card{flex:1;min-width:200px;background:#fff;border-radius:8px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,.05);}
.stat-card .num{font-size:28px;font-weight:700;color:#333;margin:4px 0;}
.stat-card .label{color:#999;font-size:14px;}
.card{background:#fff;border-radius:8px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,.05);margin-bottom:20px;}
.card h3{font-size:17px;margin-bottom:14px;color:#333;padding-bottom:10px;border-bottom:1px solid #eee;}
.app-row{display:flex;gap:16px;flex-wrap:wrap;margin-bottom:20px;}
.app-card{flex:1;min-width:280px;background:#fff;border-radius:8px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,.05);}
.app-card .app-name{font-size:16px;font-weight:600;color:#333;margin-bottom:8px;}
.app-card .app-id{color:#888;font-size:13px;word-break:break-all;}
.app-card .app-meta{margin-top:10px;font-size:13px;color:#666;}
.app-card .app-meta span{margin-right:16px;}
.layui-table td, .layui-table th{padding:8px 10px;font-size:13px;}
</style>
</head>
<body>
<div class="header"><div class="inner">
    <a href="/" class="logo"><?= $systemConfig['system_name'] ?? '实名认证分发系统' ?></a>
    <div class="nav">
        <a href="/user/index/dashboard" class="active">控制台</a>
        <a href="/user/index/packages">套餐市场</a>
        <a href="/user/index/myApps">我的应用</a>
        <a href="/user/index/callStats">调用统计</a>
        <a href="/user/index/myOrders">我的订单</a>
        <a href="/user/index/myPackages">已购套餐</a>
        <a href="/user/index/recharge">余额充值</a>
        <a href="/user/index/apiDoc">接入文档</a>
        <a href="/user/index/profile"><?= $userInfo['nickname'] ?? $userInfo['username'] ?></a>
        <a href="/user/index/logout">退出</a>
    </div>
</div></div>
<div class="container">
    <?php if(!empty($systemConfig['system_notice'])): ?>
    <div class="card" style="background:#fffbe6;border-left:3px solid #ffb800;">
        <b style="color:#ff8c00;">📢 系统公告：</b><?= htmlspecialchars($systemConfig['system_notice']) ?>
    </div>
    <?php endif; ?>
    <div class="stat-row">
        <div class="stat-card"><div class="label">我的应用</div><div class="num"><?= count($apps) ?></div></div>
        <div class="stat-card"><div class="label">累计调用</div><div class="num"><?= $totalCalls ?></div></div>
        <div class="stat-card"><div class="label">今日调用</div><div class="num"><?= $todayCalls ?></div></div>
        <div class="stat-card"><div class="label">账户余额</div><div class="num">¥<?= number_format($userInfo['balance'] ?? 0, 2) ?></div></div>
    </div>
    <div class="card">
        <h3>我的应用</h3>
        <?php if(!empty($apps)): ?>
        <div class="app-row">
            <?php foreach($apps as $app): ?>
            <div class="app-card">
                <div class="app-name"><?= $app['name'] ?></div>
                <div class="app-id">AppID: <?= $app['app_id'] ?></div>
                <div class="app-meta">
                    <span>剩余: <b><?= $app['remain_calls'] ?></b> 次</span>
                    <span>状态: <?= $app['status'] ? '<span style="color:green">正常</span>' : '<span style="color:red">禁用</span>' ?></span>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        <?php else: ?>
        <div style="text-align:center;padding:30px;color:#999;">还没有应用，<a href="/user/index/myApps" style="color:#1e9fff;">去创建</a></div>
        <?php endif; ?>
    </div>
    <div class="card">
        <h3>已购套餐</h3>
        <?php if(!empty($myPackages)): ?>
        <table class="layui-table" lay-size="sm">
            <thead><tr><th>套餐名称</th><th>购买时间</th><th>到期时间</th><th>状态</th></tr></thead>
            <tbody>
                <?php foreach($myPackages as $pkg): ?>
                <tr>
                    <td><?= $pkg['package_name'] ?></td>
                    <td><?= $pkg['pay_time'] ?? $pkg['create_time'] ?></td>
                    <td><?= date('Y-m-d H:i:s', $pkg['expire_ts']) ?></td>
                    <td><?= $pkg['expired'] ? '<span style="color:red">已过期</span>' : '<span style="color:green">有效</span>' ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <?php else: ?>
        <div style="text-align:center;padding:30px;color:#999;">暂无已购套餐，<a href="/user/index/packages" style="color:#1e9fff;">去购买</a></div>
        <?php endif; ?>
    </div>
    <div class="card">
        <h3>最近调用日志</h3>
        <table class="layui-table" lay-size="sm">
            <thead><tr><th>AppID</th><th>接口</th><th>状态</th><th>IP</th><th>时间</th></tr></thead>
            <tbody>
                <?php foreach($recentLogs as $log): ?>
                <tr><td><?= $log['app_id'] ?></td><td><?= $log['api_title'] ?: $log['api_name'] ?></td><td><?= $log['status'] ? '<span style="color:green">成功</span>' : '<span style="color:red">失败</span>' ?></td><td><?= $log['ip'] ?></td><td><?= $log['create_time'] ?></td></tr>
                <?php endforeach; ?>
                <?php if(empty($recentLogs)): ?><tr><td colspan="5" style="text-align:center;color:#999;">暂无调用记录</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>